package cn.zxl.spring.holiday.job.web.log;

import cn.zxl.spring.holiday.job.domain.OperateLog;
import cn.zxl.spring.holiday.job.service.OperateLogService;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Date 2021/9/23 14:23
 * @Version 1.0
 * @Description
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    private OperateLogService service;
    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("@annotation(cn.zxl.spring.holiday.job.web.log.Log)")
    public void logPointCut() {
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint  切点
     * @param jsonResult 返回结果
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
        // TODO 请完成相关功能
        // 目标对象（访问哪个对象的方法就是哪个对象）
        Object target = joinPoint.getTarget();
        OperateLog log = service.getLog();
        log.setJsonResult(JSON.toJSONString(jsonResult));
        // 方法签名，获取方法相关信息
        Signature signature = joinPoint.getSignature();
        String s1 = signature.toString();
        String[] s2 = s1.split(" ");
        String signatureName = s2[1];
        System.out.println(signatureName);
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Log annotation = method.getAnnotation(Log.class);
        log.setStatus(0);
        log.setTitle(annotation.title());
        log.setBusinessType(annotation.businessType());
        // 获取声明类型
        String signaturen = signatureName.split("controller.")[1];
        log.setMethod(signaturen);
        System.out.println(log);
        log.setOperTime(new Date());
//        log.setId(1L);
        boolean b = service.saveTote(log);
    }

    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
        // TODO 请完成相关功能
        // 目标对象（访问哪个对象的方法就是哪个对象）
        Object target = joinPoint.getTarget();
        OperateLog log = service.getLog();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Log annotation = method.getAnnotation(Log.class);
        log.setStatus(1);
        log.setTitle(annotation.title());
        log.setBusinessType(annotation.businessType());
        // 获取声明类型
        String s1 = signature.toString();
        String[] s2 = s1.split(" ");
        String signatureName = s2[1];
        System.out.println(signatureName);
        log.setRequestMethod(signatureName);
        log.setErrorMsg(e.getMessage());
        System.out.println(log);
        log.setOperTime(new Date());
    }

}
